<?php

/**
 * 首页路由处理
 * 
 * 
 */

//设置时区
date_default_timezone_set('Asia/Shanghai');

//设置入口标志
define('IN_ACHAO_ORG',  TRUE);
define('ROOT_PATH',     dirname(__FILE__));
define('PAGE_STARTED',  (PHP_VERSION >= '5.0.0') ? microtime(true) : microtime());
//判断请求方式 
define('IS_POST', (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST'));


$php_self = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : $_SERVER['SCRIPT_NAME'];
if ('/' == substr($php_self, -1))
{
    $php_self .= 'store.php';
}
define('PHP_SELF',  htmlentities($php_self));
define('ROOT_DIR',  substr(PHP_SELF, 0, strrpos(PHP_SELF, '/')));
define('ROOT_DIR2', ROOT_DIR.'/');

$query_string = isset($_SERVER['argv'][0]) ? $_SERVER['argv'][0] : $_SERVER['QUERY_STRING'];
if (!isset($_SERVER['REQUEST_URI']))
{
    $_SERVER['REQUEST_URI'] = PHP_SELF . '?' . $query_string;
}
else
{
    if (strpos($_SERVER['REQUEST_URI'], '?') === false && $query_string)
    {
        $_SERVER['REQUEST_URI'] .= '?' . $query_string;
    }
}

//include_once(ROOT_PATH .'/includes/var.map.php');
//require(ROOT_PATH. '/appService/appService.php');
require(ROOT_PATH. '/includes/models/mod.base.php');
require(ROOT_PATH. '/includes/manager/mng.base.php');
require(ROOT_PATH. '/includes/ctl.frontend.php');
require(ROOT_PATH. '/includes/inc.init.php');

if($_REQUEST['app']=='ajax')
    $ajax = true;
else
    $ajax = false;


$act = isset($_REQUEST['act']) ? trim($_REQUEST['act']): '';
$app = isset($_REQUEST['app']) ? basename($_REQUEST['app']): 'mall';
$recapp = isset($_REQUEST['recapp']) ? trim($_REQUEST['recapp']): 'mall';


if (isset($_REQUEST['app']))
{
    $app = basename($_REQUEST['app']);
}
else
{
    $app = 'mall';
    if ($_SERVER['REQUEST_METHOD'] == 'GET' && count($_GET) == 1)
    {
        $arr = array_keys($_GET);

        if (isset($_GET['store_id']))
        {
            $app = 'store';
        }
        elseif (is_int($arr[0]))
        {
            $app = 'store';
            $_GET['store_id'] = $arr[0];
        }
    }
}

if (defined('ENABLED_CUSTOM_DOMAIN') && ENABLED_CUSTOM_DOMAIN && (!is_main_site()) && (!IS_AJAX))
{
    if (!in_array($app, array('goods','goods', 'groupbuy', 'pm', 'regions', 'respond', 'mail', 'issue', 'crontab', 'datacall','myapp','myppindex')))
    {
        if (in_array($app, array('mall', 'store')))
        {
            $prefix_domain = get_prefix_domain();

            include_once(ROOT_PATH . '/includes/manager/mng.store.php');
            $store_id = StoreManager::get_store_id_by_custom($prefix_domain);
            if ($store_id > 0)
            {
                $app = 'store';
                $_GET['app'] = $app;
                $_GET['store_id'] = $store_id;
            }
            else
            {
                header('Location: ' . MAIN_DOMAIN . "\n");
                exit;
            }
        }
        else
        {
            $add_param = empty($query_string) ? '' : '/index.php?' . $query_string;
            header('Location: ' . trim(MAIN_DOMAIN, '/') . $add_param . "\n");
            exit;
        }
    }
}

//允许访问的APP
$allowed_app = array('mall','hotel','article', 'help', 'about');

$table_flag = array("index" => array('mall','appsort','trends','apphelp', 'mymachine'),
                    'store' => array("store", "storeindex", "storelist", "storeapply"),
                    'apps'=>array('apps', 'goods', 'search'),
                );
//echo $app;
if (!in_array($app, $allowed_app))
{
    die('Hack Attemping.');
}
//session_start();
$_SESSION['main_url'] = MAIN_DOMAIN;
$_SESSION['class_index'] = '';



if($app == 'service')
{
    $light = $recapp;
}
else
{
	$sort_id= $_GET['sort_id'];
    $light = $app;

    if (in_array($light, $table_flag['store']))
        $_SESSION['class_index'] = 'store';
    elseif (in_array($light, $table_flag['apps']))
    {
    	if($sort_id)
    	{
    	   $_SESSION["class_index"] = get_cate_by_act($sort_id);
    	}
    	else 
    	{
    		$_SESSION["class_index"] = $act;
    	}	
    }
    elseif ($light == 'member')
        $_SESSION['class_index'] = 'member';
    else
        $_SESSION['class_index'] = 'index';
}

$_SESSION['rec_app'] = $app;    
define('APPLICATION', $app);


$app_file   = ROOT_PATH . '/' . APPLICATION . '.php';
$app_class  = ucfirst(APPLICATION) . 'Controller';

require($app_file);
$controller = new $app_class($act);
$controller->destory();

function get_prefix_domain()
{
    $domain = get_domain();
    $tmp_arr = parse_url($domain);
    $host = $tmp_arr['host'];
    $tmp_arr = explode('.', $host, 3);
    if ($tmp_arr[0] == 'www')
    {
        $prefix_domain = $tmp_arr[1];
    }
    else
    {
        $prefix_domain = $tmp_arr[0];
    }

    return $prefix_domain;
}
